﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AmiEmu
{
    public partial class InputForm : Form
    {
        public InputForm()
        {
            InitializeComponent();
        }

        public static void Create(string a_title, string a_label, Action<string> a_ok, 
            string a_default = "", Func<string, string> a_validation = null, 
            bool a_validation_disable_ok = true)
        {
            InputForm input = new InputForm();
            input.label.Text = a_label;
            input.textBox.Text = a_default;
            input.Text = a_title;

            input.errorProvider.SetIconAlignment(input.textBox, ErrorIconAlignment.MiddleRight);
            input.errorProvider.SetIconPadding(input.textBox, 2);

            input.textBox.TextChanged += (sender, arg) =>
            {
                if (a_validation != null)
                {
                    string err = a_validation(input.textBox.Text);

                    if (err == null)
                    {
                        input.errorProvider.SetError(input.textBox, "");
                        input.OKButton.Enabled = true;
                    }
                    else
                    {
                        if (err != "")
                            input.errorProvider.SetError(input.textBox, err);
                        input.OKButton.Enabled = false;
                    }
                }
            };

            input.ActiveControl = input.textBox;

            if (input.ShowDialog() == DialogResult.OK)
                a_ok(input.textBox.Text);
        }
    }
}
